package com.tarena.rpc.demo01.provider;

import com.tarena.rpc.common.People;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

//模拟的是dubbo的provider端
//在监听一个端口 等待consumer调用
//调用者只知道接口 不知道实现
public class Provider01 {
    public static void main(String[] args) throws Exception {
        //对外暴露的可访问的端口8000
        ServerSocket serverSocket=new ServerSocket(8000);
        //持续监听
        while(true){
            Socket socket = serverSocket.accept();//接收请求
            process(socket);//处理请求
            socket.close();
        }
    }

    private static void process(Socket socket) throws Exception {
        //拿到输入流
        InputStream inputStream = socket.getInputStream();//数据就是传递参数做得序列化
        //对方传递的参数 是方法String clothName
        DataInputStream dis=new DataInputStream(inputStream);
        String clothName = dis.readUTF();
        System.out.println("拿到对方传递的参数");
        People people=new PeopleImpl();
        String wash = people.wash(clothName);
        //socket实现输出流
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dos=new DataOutputStream(outputStream);
        dos.writeUTF(wash);
        dos.flush();
    }
}
